﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Web.User.Exchange.Report
{
    public partial class ExchangeReport : System.Web.UI.Page
    {
     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();
            }
        }
        private void DataBind()
        {

            common.IsLogin();
            string WBID = Session["WB_ID"].ToString();
            //获取存粮信息
            StringBuilder strSql = new StringBuilder();
            strSql.Append("  SELECT B.strName AS WBID, D.strName AS  VarietyID, E.strName AS  GoodID, SUM(GoodCount) AS GoodCount,SUM(VarietyCount)AS VarietyCount,");
            strSql.Append("   SUM(A.Money_DuiHuan) AS Money_DuiHuan,SUM(A.VarietyInterest) AS VarietyInterest");
            strSql.Append("  FROM dbo.GoodExchange A INNER JOIN dbo.WB B ON A.WBID=B.ID");
            strSql.Append("  INNER JOIN dbo.Dep_StorageInfo C ON A.Dep_SID=C.ID");
            strSql.Append("   INNER JOIN dbo.StorageVariety D ON C.VarietyID=D.ID");
            strSql.Append("  INNER JOIN dbo.Good E ON A.GoodID=E.ID");
            strSql.Append("  ");
            strSql.Append("  where 1=1");
            strSql.Append("  and A.WBID="+WBID);
           
            if (Qdtstart.Value.Trim() != "")
            {
                strSql.Append("   AND A.dt_Exchange> '" + Qdtstart.Value.Trim() + "'");
            }
            if (Qdtend.Value.Trim() != "")
            {
                strSql.Append("   AND A.dt_Exchange < '" + Qdtend.Value.Trim() + "'");
            }

            strSql.Append("   GROUP BY B.strName, E.strName,D.strName");
            strSql.Append("  ");


            DataTable dt = SQLHelper.ExecuteDataTable(strSql.ToString());
            Repeater1.DataSource = dt;
            Repeater1.DataBind();



            StringBuilder strSql2 = new StringBuilder();
            strSql2.Append("  SELECT  D.strName AS  VarietyID,SUM(GoodCount) AS GoodCount,SUM(VarietyCount)AS VarietyCount,");
            strSql2.Append("   SUM(A.Money_DuiHuan) AS Money_DuiHuan,SUM(A.VarietyInterest) AS VarietyInterest");
            strSql2.Append("  FROM dbo.GoodExchange A INNER JOIN dbo.WB B ON A.WBID=B.ID");
            strSql2.Append("  INNER JOIN dbo.Dep_StorageInfo C ON A.Dep_SID=C.ID");
            strSql2.Append("   INNER JOIN dbo.StorageVariety D ON C.VarietyID=D.ID");
            strSql2.Append("  INNER JOIN dbo.Good E ON A.GoodID=E.ID");
            strSql2.Append("  where 1=1");
            strSql2.Append("  and A.WBID=" + WBID);
          
            if (Qdtstart.Value.Trim() != "")
            {
                strSql2.Append("   AND A.dt_Exchange> '" + Qdtstart.Value.Trim() + "'");
            }
            if (Qdtend.Value.Trim() != "")
            {
                strSql2.Append("   AND A.dt_Exchange < '" + Qdtend.Value.Trim() + "'");
            }

            strSql2.Append("   GROUP BY  D.strName");
            strSql2.Append("  ");


            DataTable dt2 = SQLHelper.ExecuteDataTable(strSql2.ToString());
            Repeater2.DataSource = dt2;
            Repeater2.DataBind();
            //合计数据


        }



        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {

            DataBind();

        }
    }
}